/**
 * Created by Ynlh on 15/10/21.
 */
var Grid = cc.Scale9Sprite.extend({

    size : 4,
    //cells : previousState ? this.fromState(previousState) : this.empty(),

    ctor : function () {
        this._super(res.bg_grid);

        this.attr({
            x : cc.winSize.width / 2,
            y : cc.winSize.height / 2 + 40,
            width : 500,
            height : 500,
            color : cc.color("#bbada0"),
            cascadeColor : false
        });

        this.cells = new Array(this.size);
        for (var x = 0; x < this.size; x++) {
            this.cells[x] = new Array(this.size);

            for (var y = 0; y < this.size; y++) {
                var tile = new Tile({x : x, y : y});
                this.addChild(tile.sprite);
            }
        }
    },

    //initCells : function () {
    //    var cells = [];
    //    for (var x = 0; x < this.size; x++) {
    //        var row = cells[x] = [];
    //        for (var y = 0; y < this.size; y++) {
    //            row.push(null);
    //        }
    //    }
    //    return cells;
    //},

    empty : function () {
        for (var x = 0; x < this.size; x++) {
            for (var y = 0; y < this.size; y++)
                this.removeTile(this.cells[x][y])
        }
    },

    fromState : function (state) {
        var cells = [];
        for (var x = 0; x < this.size; x++) {
            var row = cells[x] = [];

            for (var y = 0; y < this.size; y++) {
                var tile = state[x][y];
                row.push(tile ? new Tile(tile.position, tile.value) : null);
            }
        }
        return cells;
    },

    randomAvailableCell : function () {
        var cells = this.availableCells();
        if (cells.length) {
            return cells[Math.floor(Math.random() * cells.length)];
        }
    },

    availableCells : function () {
        var cells = [];
        this.eachCell(function (x, y, tile) {
            if (!tile) {
                cells.push({x : x, y : y});
            }
        });
        return cells;
    },

    eachCell : function (callback) {
        for (var x = 0; x < this.size; x++) {
            for (var y = 0; y < this.size; y++) {
                callback(x, y, this.cells[x][y]);
            }
        }
    },

    // Check if there are any cells available
    // 检查是否还有空得格子
    cellsAvailable : function () {
        return !!this.availableCells().length;
    },


    // Check if the specified cell is taken
    cellAvailable : function (cell) {
        return !this.cellOccupied(cell);
    },

    cellOccupied : function (cell) {
        return !!this.cellContent(cell);
    },

    cellContent : function (cell) {
        if (this.withinBounds(cell)) {
            return this.cells[cell.x][cell.y];
        } else {
            return null;
        }
    },

    addTile : function (tile) {
        this.cells[tile.x][tile.y] = tile;
        this.addChild(this.cells[tile.x][tile.y].sprite, 2);
    },

    removeTile : function (tile) {
        if (tile) {
            this.removeChild(this.cells[tile.x][tile.y].sprite);
            this.cells[tile.x][tile.y] = null;
        }
    },

    withinBounds : function (position) {
        return position.x >= 0 && position.x < this.size &&
            position.y >= 0 && position.y < this.size;
    },

    //serialize : function () {
    //    var cellState = [];
    //
    //    for (var x = 0; x < this.size; x++) {
    //        var row = cellState[x] = [];
    //
    //        for (var y = 0; y < this.size; y++) {
    //            row.push(this.cells[x][y] ? this.cells[x][y].serialize() : null);
    //        }
    //    }
    //
    //    return {
    //        size : this.size,
    //        cells : cellState
    //    };
    //}
});